extends Control

@onready var hp_full: NinePatchRect = %HP_Full
@onready var hp: NinePatchRect = %HP
@onready var viewp: SubViewport = $MarginContainer/VBoxContainer/MarginContainer/View/Viewp
@onready var mapview: Camera2D = $MarginContainer/VBoxContainer/MarginContainer/View/Viewp/Mapview
@onready var body: Sprite2D = $MarginContainer/VBoxContainer/MarginContainer/View/Viewp/Body
@onready var hp_display: Label = $MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer/HP_Display
@onready var options: TextureButton = $MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/VBoxContainer/MarginContainer2/Options

@export var player : CharacterBody2D
@export var tilemap : TileMap
@export var interface_back : C_Interface

signal open_bag
signal died

var save_name : String = "Info"

var kills : int = 0


var MinimapPlayer
# Called when the node enters the scene tree for the first time.
func _ready():
	hp_display.text = str(interface_back.player_hp)+'/'+str(interface_back.player_max_hp)
	init_body()
	hp_full.custom_minimum_size.x = interface_back.player_max_hp
	hp.custom_minimum_size.x = (float(interface_back.player_hp)/float(interface_back.player_max_hp))*hp_full.custom_minimum_size.x
	# MinimapPlayer = player.duplicate()
	viewp.add_child(tilemap.duplicate())
	# viewp.add_child(MinimapPlayer)
	pass # Replace with function body.


func init_body():
	body.z_index = 18
	body.scale = Vector2(0.03,0.03)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	# MinimapPlayer.position = player.position
	body.position = player.position
	mapview.position = player.position

func get_scores()->int:
	kills+=1
	return kills

func set_scores(val : int):
	kills = val


func _on_bag_button_up() -> void:
	open_bag.emit()
	pass # Replace with function body.
	

func player_hurted() -> void:
	hp.custom_minimum_size.x -= hp_full.custom_minimum_size.x/4
	hp_display.text = str(hp.custom_minimum_size.x)+'/'+str(interface_back.player_max_hp)
	if hp.custom_minimum_size.x<=0:
		died.emit()
	pass # Replace with function body.


func get_save_name()->String:
	return save_name

func save_data():
	var dict = {
		"node" : "Info",
		"kills": kills,
	}
	return dict

func load_data(data):
	kills = data.get("kills",0)

